package cc.linker.steplink.crm.merchant.service;
import cc.linker.steplink.crm.merchant.dao.FreightTemplateDao;
import cc.linker.steplink.crm.merchant.dao.FreightAreaDao;
import cc.linker.steplink.crm.merchant.pojo.FreightTemplatePo;
import cc.linker.steplink.crm.merchant.pojo.FreightAreaPo;
import cc.linker.steplink.crm.merchant.service.redis.FreightTemplateRedisService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @Author: CKQ
 * @DateTime: 2020/5/22 18:18
 * @Description: TODO
 */

@Service("FreightTemplateService")
public class FreightTemplateService {
    @Autowired
    private FreightTemplateDao freightTemplateDao;

    @Autowired
    private FreightAreaDao freightAreaDao;

    @Autowired
    private FreightTemplateRedisService freightTemplateRedisService;

    //获取运费模板列表
    public PageInfo<FreightTemplatePo> getFreightTemplatePageList(Integer pageIndex, Integer pageSize, String customerId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<FreightTemplatePo> templatePos = freightTemplateDao.getFreightTemplateList(customerId);
        for (FreightTemplatePo po : templatePos) {
//            po.freightAreaPos = freightAreaDao.getFreightAreaList(po.templateId);
            po.quoteCount = freightTemplateDao.getQuoteCount(po.templateId);
        }
        return new PageInfo<>(templatePos);
    }

    //获取运费模板
    public  List<FreightTemplatePo> getFreightTemplateList(String customerId) {
        List<FreightTemplatePo> templatePos = freightTemplateDao.getFreightTemplateList(customerId);
        return templatePos;
    }
    //获取运费模板详情
    public  FreightTemplatePo getFreightTemplateModel(String templateId) {
        FreightTemplatePo po = freightTemplateDao.getFreightTemplateModel(templateId);
        po.freightAreaPos = freightAreaDao.getFreightAreaList(po.templateId);
        return po;
    }

    //添加运费模板
    public boolean insertFreightTemplate(FreightTemplatePo info) {
        if (freightTemplateDao.isExitTemplate(info.templateId, info.templateName, info.customerId) > 0) {
            throw GlobalException.build("模板名称不能重复");
        }
        Boolean isAdd = false;
        if (org.springframework.util.StringUtils.isEmpty(info.templateId)) {
            info.templateId = UUID.randomUUID().toString();
            isAdd = true;
        }
        Integer isSave = 0;
        if(isAdd){
            isSave = freightTemplateDao.insertFreightTemplate(info);
        }else{
            isSave = freightTemplateDao.updateFreightTemplate(info);
        }
        if ( isSave > 0) {
            //遍历配送区域组装数据
            for (FreightAreaPo po : info.freightAreaPos) {
                po.templateId = info.templateId;
            }
            //添加所有配送区域信息
            freightAreaDao.deleteFreightAreaByTemplateId(info.templateId);
            if (freightAreaDao.insertListFreightArea(info.freightAreaPos) == 0) {
                throw GlobalException.build("保存运费模板失败");
            }
        } else {
            throw GlobalException.build("保存运费模板失败");
        }
        //freightTemplateRedisService.setFreightTemplateRedis(info.templateId);
        return true;
    }

    //删除模板
    public boolean removeFreightTemplate(String templateId) {
        Long goodsCount = freightTemplateDao.getQuoteCount(templateId);
        if (goodsCount > 0) {
            throw GlobalException.build("该模板已被" + goodsCount + "个商品使用，不允许删除");
        }
        Boolean isAddFreight = freightTemplateDao.removeFreightTemplate(templateId) > 0;

        //freightTemplateRedisService.deleteFreightTemplateRedis(templateId);
        return  isAddFreight;
    }
}
